package controller;

/*
 * Action for researchers to input question for the questionnaire.
 * Used by both Binary and Network Binary type questions.
 */

import formbeans.SurveyTitleForm;
import helper.EntityType;
import helper.UserType;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.mybeans.dao.DAOException;
import org.mybeans.form.FormBeanException;
import org.mybeans.form.FormBeanFactory;

import databeans.EntityClass;
import databeans.Survey;
import databeans.User;
import model.EntityClassDAO;
import model.MailingListDAO;
import model.Model;
import model.SurveyDAO;
import model.UserDAO;

public class SurveyTitleAction extends Action {
	private FormBeanFactory<SurveyTitleForm> formBeanFactory = FormBeanFactory.getInstance(SurveyTitleForm.class);
	private SurveyDAO surveyDAO;
	private UserDAO userDAO;
	private MailingListDAO mlDAO;
	private EntityClassDAO ecDAO;
	
	public SurveyTitleAction(Model model) {
		surveyDAO = model.getSurveyDAO();
		userDAO = model.getUserDAO();
		mlDAO = model.getMailingListDAO();
		ecDAO = model.getEntityClassDAO();
	}
	
    public String getName() {
		return "surveytitle.do";
	}
    
    public String perform(HttpServletRequest request) {

        List<String> errors = new ArrayList<String>();
        request.setAttribute("errors",errors);
        
    	// First, validate whether researcher is accessing this
    	HttpSession session = request.getSession(false);
    	User user = (User) session.getAttribute("user");
    	
    	if (user.getType() != UserType.RESEARCHER) {
    		errors.add("You are not a researcher!");
    		return "homepage.jsp";
    	}
    	

		String button = request.getParameter("button");
		
		if (button == null)
			return "surveytitle.jsp";
				
		if (button.equals("Submit")) {
	        try {
        		SurveyTitleForm form = formBeanFactory.create(request);
            	request.setAttribute("surveytitleform",form);
            	
            	if (!form.isPresent()) {
            		return "researcher.jsp";
            	}
            	
        		// Validate for errors in the form
        		errors.addAll(form.checkSurveyTitleFormErrors());
        		
        		if (errors.size() != 0) {
        			return "researcher.jsp";
        		}
        		
        		Survey survey = new Survey(surveyDAO.getSid());
        		survey.setUserid(user.getUserid());
        		survey.setTitle(form.getTitle());
        		survey.setSent(0);
        		survey.setMailinglist("");
        		surveyDAO.create(survey, userDAO, mlDAO);

        		Survey[] old_survey = surveyDAO.getUserSurveys(user.getUserid());
        		
        		//Create an entity class for respondents
        		EntityClass ec = new EntityClass(ecDAO.getEcid());
        		ec.setName("Respondents");
        		ec.setSid(survey.getSid());
        		ec.setType(EntityType.AGENT);
        		ecDAO.create(ec, surveyDAO);
        		
        		synchronized(session) {
        			session.setAttribute("survey", survey);
        			session.setAttribute("survey_list", old_survey);
        		}
        		
        		return "createsurvey.jsp";
	        } catch (DAOException e) {
	        	errors.add(e.getMessage());
	        	return "errors.jsp";
	        } catch (FormBeanException e) {
	        	errors.add(e.getMessage());
	        	return "errors.jsp";
	        }
        }
        errors.add("Illegal button value of " + button);
		return "errors.jsp";
    }
}
